package com.jd.dl.service;

import java.util.List;

import com.jd.dl.dao.TaskDAO;
import com.jd.dl.dao.UserDAO;
import com.jd.dl.pojo.Task;
import com.jd.dl.pojo.User;
import com.jd.dl.service.exception.ServiceException;
import com.jd.dl.util.Pager;

public class TaskService {
	
	private UserDAO userDAO;
	private TaskDAO taskDAO;
	
	
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public TaskDAO getTaskDAO() {
		return taskDAO;
	}
	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}

	@SuppressWarnings("unchecked")
	public List<Task> getList(String filter) {
		return taskDAO.getByQuery(filter);
	}

	@SuppressWarnings("unchecked")
	public List<Task> getList(String filter, String sort) {
		return taskDAO.getByQuery(filter, sort);
	}

	@SuppressWarnings("unchecked")
	public List<Task> getList(Pager pager, String filter, String sort) {
		return taskDAO.getByQuery(pager, filter, sort);
	}
	
	public Task get(long id) {
		return (Task) taskDAO.getById(id);
	}
//
//	public User load(long id) {
//		User user = (User) userDAO.loadById(id);
//		List<Permission> per = user.getRole().getPermissions();
//		for (Permission permission : per) {
//			userDAO.initialize(permission);
//		}
//		return user;
//	}
//
//	
//
	public void add(final Task task) throws ServiceException {
//		HqlCriterions hc = new HqlCriterions();
//		hc.and(QueryConstants.Task.TASK_NAME, user.getLoginId(),
//				QueryConstants.EQ);
//
//		if (taskDAO.getCountByQuery(hc.getHqlString()) > 0) {
//			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY_USER, 
//					new String[]{user.getLoginId()});
//			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
//		}

		Task newTask = new Task();
		newTask.setTaskName(task.getTaskName());
		newTask.setTaskAddress(task.getTaskAddress());
		newTask.setTaskDetails(task.getTaskDetails());
		newTask.setTaskStatus(task.getTaskStatus());
		newTask.setTowerType(task.getTowerType());
		newTask.setStartTime(task.getStartTime());
		newTask.setEndTime(task.getEndTime());
		User user = (User)userDAO.getById(task.getUser().getUserId());
		newTask.setUser(user);
		newTask.setLat(task.getLat());
		newTask.setLon(task.getLon());
		
		taskDAO.add(task);

	}

	public void update(final Task task) throws ServiceException {
//		HqlCriterions hc = new HqlCriterions();
//		hc.and(QueryConstants.User.loginId, user.getLoginId(),
//				QueryConstants.EQ);
//		hc.and(QueryConstants.User.userId, user.getUserId(), QueryConstants.NE);
//
//		if (userDAO.getCountByQuery(hc.getHqlString()) > 0) {
//			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY, 
//					new String[]{user.getLoginId()});
//			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
//		}

//		User newUser = (User) userDAO.loadById(user.getUserId());


		taskDAO.saveOrUpdate(task);
	

	}
	
	public void delete(Long id) {
		Task task = (Task) taskDAO.loadById(id);
		if (task != null) {
			taskDAO.delete(task);
		}
	}
//
//
//
//	public boolean hasPermission(long id, long functionId) {
//		User user = (User) userDAO.getById(id);
//		for (Permission permission : user.getRole().getPermissions()) {
//			if (functionId == permission.getFunction().getFunctionId()) {
//				return true;
//			}
//		}
//		return false;
//	}
//
	public void deleteAll() {
		taskDAO.deleteAll();
		
	}
}
